package com.jdk8.stream;

import com.alibaba.fastjson.JSONObject;

import java.util.*;
import java.util.stream.Collectors;

public class Lesson2 {

    public static void main(String[] args) {

        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 89, 29, "male", "New York"));
        personList.add(new Person("Jack", 70, 37, "male", "Washington"));
        personList.add(new Person("Lily", 78, 22, "female", "Washington"));
        personList.add(new Person("Anni", 82, 25,"female", "New York"));
        personList.add(new Person("Owen", 95, 34,"male", "New York"));
        personList.add(new Person("Alisa", 79, 31,"female", "New York"));

        //从员工集合中筛选出salary大于8000的员工，并放置到新的集合里。
        List<Person> people = personList.stream().filter(p -> p.getSalary() > 80).collect(Collectors.toList());
        System.out.println(JSONObject.toJSONString(people));

        //统计员工总数
        Long count1 = personList.stream().count();
        Long count2 = personList.stream().collect(Collectors.counting());
        System.out.println("员工总数为 count1 : " + count1);
        System.out.println("员工总数为 count2 : " + count2);

        //统计员工平均工资
        Double average = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
        System.out.println("平均薪资 average : " + average);

        //统计员工的最高薪资
        Optional<Person> maxOptional1 = personList.stream().max(Comparator.comparingInt(Person::getSalary));
        Optional<Integer> maxOptional2 = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compareTo));
        Optional<Integer> maxOptional4 = personList.stream().map(Person::getSalary).collect(Collectors.maxBy((x,y) -> (x < y) ? -1 : ((x == y) ? 0 : 1)));

        Integer maxOptional3 = personList.stream().map(Person::getSalary).reduce(1, Integer::max);
        System.out.println("薪资最高的人 maxOptional1 : " + JSONObject.toJSONString(maxOptional1.get()));
        System.out.println("最高薪资 maxOptional2 : " + maxOptional2.get());
        System.out.println("最高薪资 maxOptional4 : " + maxOptional4.get());
        System.out.println("最高薪资 maxOptional3 : " + maxOptional3);

        //统计员工的总薪资
        Optional<Integer> sumOptional1 = personList.stream().map(Person::getSalary).reduce((x, y) -> x + y);
        Optional<Integer> sumOptional2 = personList.stream().map(Person::getSalary).reduce(Integer::sum);
        Integer sumOptional3 = personList.stream().map(Person::getSalary).reduce(0, Integer::sum);
        Integer sumOptional4 = personList.stream().collect(Collectors.summingInt(Person::getSalary));

        System.out.println("总薪资 sumOptional1 : " + sumOptional1.get());
        System.out.println("总薪资 sumOptional2 : " + sumOptional2.get());
        System.out.println("总薪资 sumOptional3 : " + sumOptional3);
        System.out.println("总薪资 sumOptional4 : " + sumOptional4);

        //一次统计员工总数，平均工资，最高工资，最低工资，总工资
        DoubleSummaryStatistics doubleSummaryStatistics = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
        IntSummaryStatistics intSummaryStatistics = personList.stream().collect(Collectors.summarizingInt(Person::getSalary));
        System.out.println("doubleSummaryStatistics : " + JSONObject.toJSONString(doubleSummaryStatistics));
        System.out.println("intSummaryStatistics : " + JSONObject.toJSONString(intSummaryStatistics));

        //将员工按薪资是否高于80分为两部分
        Map<Boolean, List<Person>> map1 = personList.stream().collect(Collectors.partitioningBy(person -> person.getSalary() > 80));
        Map<String, List<Person>> map2 = personList.stream().collect(Collectors.groupingBy(Person::getSex));
        Map<String, Long> map4 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.counting()));
        Map<String, Map<String,List<Person>>> map3 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
        Map<String, Map<String,Long>> map5 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea, Collectors.counting())));

        Map<Object, String> map = personList.stream().collect(Collectors.toMap(x -> x.getName(), Person::getArea, (x1, x2) -> x1));


        System.out.println("员工按薪资是否高于80分组 : " + JSONObject.toJSONString(map1));
        System.out.println("员工按性别分组 : " + JSONObject.toJSONString(map2));
        System.out.println("员工按性别分组,统计个数 : " + JSONObject.toJSONString(map4));
        System.out.println("员工按性别、地区分组 : " + JSONObject.toJSONString(map3));
        System.out.println("员工按性别、地区分组,统计个数 : " + JSONObject.toJSONString(map5));

    }
}
